`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2024/07/24 16:50:05
// Design Name: 
// Module Name: extend_imm
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module extend_imm(
    clk,nop,op_imm,imm_1,imm_2,imm_3,imm_4,data_2,data_imm,data_2_exe,sex_imm,sex_imm_b
    );
    input clk,nop;
    input [2:0] op_imm;
    input [4:0] imm_1;
    input [11:0] imm_2;
    input [6:0] imm_3;
    input [19:0] imm_4;
    input [31:0] data_2;
    output reg [31:0] data_imm,sex_imm,sex_imm_b;
    output reg [31:0] data_2_exe;

    parameter R_type=0,I_type=1,S_type=2,B_type=3,J_type=4;

    always @(posedge clk) begin
        if(nop) begin
            data_2_exe <= 32'bx;
            sex_imm_b <= 32'bx;
        end
        else begin
            data_2_exe <= data_2;
            sex_imm_b <= sex_imm;
        end
    end

    always @(*) begin
        case (op_imm)
            R_type : begin 
                sex_imm = data_2;
            end
            I_type : begin
                sex_imm = {{20{imm_2[11]}},imm_2};
            end
            S_type : begin
                sex_imm = {{20{imm_3[6]}},imm_3,imm_1};
            end
            B_type : begin
                sex_imm = {{20{imm_3[6]}},imm_1[0],imm_3[5:0],imm_1[4:1],1'b0};
            end
            J_type : begin
                sex_imm = {{12{imm_4[19]}},imm_4[7:0],imm_4[8],imm_4[18:9],1'b0};
            end
            default : begin
                sex_imm = 32'bx;
            end
        endcase
    end

    always @(*) begin
        case (op_imm)
            R_type : begin 
                data_imm = data_2;
            end
            I_type : begin
                data_imm = sex_imm;
            end
            S_type : begin
                data_imm = sex_imm;
            end
            B_type : begin
                data_imm = data_2;
            end
            J_type : begin
                data_imm = sex_imm;
            end
            default : begin
                data_imm = 32'bx;
            end
        endcase
    end
endmodule
